﻿using Bricks.Business.Data.Repositories.QualityManagement;
using Bricks.Business.Models.QualityManagement;
using Bricks.Business.Models.SafetyManagement;
using Bricks.Component.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;

namespace Bricks.Business.Service.Impl
{
    /// <summary>
    /// 质量管理-质量整改
    /// </summary>
    [Export(typeof(IQualityCorrectiveService))]
    internal class QualityCorrectiveService : IQualityCorrectiveService
    {
        [Import]
        protected IQualityCorrectiveRepository QualityCorrectiveRepository { get; set; }

        public IQueryable<QualityCorrective> QualityCorrectives
        {
            get
            {
                return QualityCorrectiveRepository.Entities;
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public OperationResult Add(QualityCorrective model)
        {
            QualityCorrective record = model.CopyTo<QualityCorrective>();
            //record.ID = GuidHelper.NewGuid();
            QualityCorrectiveRepository.Insert(record);
            return new OperationResult(OperationResultType.Success, "新增成功", record);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public OperationResult Modify(QualityCorrective model)
        {
            PublicHelper.CheckArgument(model, "model");
            QualityCorrective record = QualityCorrectives.FirstOrDefault(m => m.ID == model.ID);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            record = model.CopyTo<QualityCorrective>();
            if (QualityCorrectiveRepository.Update(record) >= 0)
                return new OperationResult(OperationResultType.Success, "修改成功", record);
            return new OperationResult(OperationResultType.Error, "保存失败");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperationResult Delete(Guid id)
        {
            QualityCorrective record = QualityCorrectives.FirstOrDefault(m => m.ID == id);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            if (QualityCorrectiveRepository.Delete(record) >= 0)
                return new OperationResult(OperationResultType.Success, "删除成功", record);
            return new OperationResult(OperationResultType.Error, "删除失败");
        }
    }
}
